/**
 * Copyright (c) 2024 myons Inc. All rights reserved.
 */
package cn.zmwh.common.minio.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.zmwh.common.core.api.Result;
import cn.zmwh.common.core.utils.I18nUtils;
import cn.zmwh.common.minio.pojo.vo.UploadInfo;
import cn.zmwh.common.minio.config.MinioConfig;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import io.minio.errors.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

/**
 * @describe: 工具 、
 * @author: dmzmwh 、
 * @time: 2024-09-21 19:57 、
 */
@Slf4j
@Api(tags = "工具相关")
@RestController
@RequestMapping("/tool")
public class ToolController {


    @Autowired
    MinioClient minioClient;


    @Autowired
    MinioConfig minioConfig;


    @PostMapping("/upload/{folder}")
    @ApiOperation(value = "上传文件")
    public Result upload(@PathVariable("folder") String folder, @RequestParam("file") MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {

        String fileName = file.getOriginalFilename();
        String prefix = FileUtil.getSuffix(fileName);
        fileName = folder + "/" + IdUtil.getSnowflakeNextIdStr() + "." + prefix;
        String bucket = minioConfig.getBucket();
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucket)
                .object(fileName)
                .stream(file.getInputStream(), file.getSize(), -1) // -1 表示不知道大小
                .contentType(file.getContentType())  // 设置文件类型
                .build());
        String url = minioConfig.getEndpoint() + "/" + bucket + "/" + fileName;
        return Result.ok(url);
    }

    @PostMapping("/upload/info/{folder}")
    @ApiOperation(value = "上传文件")
    public Result uploadInfo(@PathVariable("folder") String folder, @RequestParam("file") MultipartFile file) {
        String fileName = file.getOriginalFilename();
        try {
            String prefix = FileUtil.getSuffix(fileName);
            String ccUrl = folder + "/" + IdUtil.getSnowflakeNextIdStr() + "." + prefix;
            String bucket = minioConfig.getBucket();
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucket)
                    .object(ccUrl)
                    .stream(file.getInputStream(), file.getSize(), -1) // -1 表示不知道大小
                    .contentType(file.getContentType())  // 设置文件类型
                    .build());
            String filePath = "/" + bucket + "/" + ccUrl;
            String url = minioConfig.getEndpoint() + filePath;

            UploadInfo uploadInfo = new UploadInfo();
            uploadInfo.setFileName(fileName.replace("." + prefix, ""));
            uploadInfo.setFilePath(filePath);
            uploadInfo.setUrl(url);
            uploadInfo.setFileSize(file.getSize());
            return Result.ok(uploadInfo);
        } catch (Exception e) {
            log.error(fileName + "上传文件失败", e);
            return Result.failed(I18nUtils.getWithDefault("upload.error", "上传失败！"));
        }
    }

    @GetMapping("/delete")
    @ApiOperation(value = "删除")
    public Result delete(String object) throws Exception {
        String bucket = minioConfig.getBucket();
        object = object.replace(minioConfig.getEndpoint() + "/" + bucket + "/", "");

        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucket)  // 替换为你的 Bucket 名称
                        .object(object) // 替换为你的对象名称
                        .build()
        );
        return Result.success();
    }
}

